package com.ruoyi.web.controller.app;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.wb.domain.WbEntAuth;
import com.ruoyi.wb.domain.WbMaintainCompany;
import com.ruoyi.wb.domain.WbMasterEnter;
import com.ruoyi.wb.service.IWbEntAuthService;
import com.ruoyi.wb.service.IWbMaintainCompanyService;
import com.ruoyi.wb.service.IWbMasterEnterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * 登录验证
 *
 * @author ruoyi
 */
@RequestMapping("/wb/app")
@RestController
public class WbAppLoginController {

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private IWbMasterEnterService iWbMasterEnterService;

    @Autowired
    private IWbEntAuthService iWbEntAuthService;



    @Autowired
    private IWbMaintainCompanyService iWbMaintainCompanyService;


    @ApiOperation(value = "获取用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        //是否入驻
        WbMasterEnter wbMasterEnter = iWbMasterEnterService.selectWbMasterEnterByUserId(user.getUserId());

        //查询认证企业
        List<WbEntAuth> wbEntAuthList= iWbEntAuthService.selectWbEntAuthListByUserId(user.getUserId());

        AjaxResult ajax = AjaxResult.success();
        WbEntAuth wbEntAuth=null;
        WbMaintainCompany maintainCompany=null;
        if(!CollectionUtils.isEmpty(wbEntAuthList)){
            wbEntAuth = wbEntAuthList.get(0);
            //查询维保公司
            List<WbMaintainCompany> wbMaintainCompanyList=iWbMaintainCompanyService.selectWbMaintainCompanyByAuthId(wbEntAuth.getId());
            if (!CollectionUtils.isEmpty(wbMaintainCompanyList)){
                maintainCompany=wbMaintainCompanyList.get(0);
            }
        }else {
            //查询维保公司
            List<WbMaintainCompany> wbMaintainCompanyList=iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(user.getUserId());
            if (!CollectionUtils.isEmpty(wbMaintainCompanyList)){
                maintainCompany=wbMaintainCompanyList.get(0);
            }
        }

        ajax.put("user", user);//用户信息
        ajax.put("roles", roles);//角色集合
        ajax.put("permissions", permissions);//权限集合
        ajax.put("wbMasterEnter", wbMasterEnter);//师傅入驻
        ajax.put("wbEntAuth",wbEntAuth);//认证企业
        ajax.put("maintainCompany",maintainCompany);//维保公司
        return ajax;
    }

}
